using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Decav.Gatsb;
using Decav.Gatsb.Web;
using Decav.Gatsb.Data;
using Decav.Gatsb.Application;
using Decav.Gatsb.Configuration;
using Decav.Reality;

public partial class Place : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Title = string.Format("{0} - gatsb.com", Location.ToString());

        if (User.Identity.IsAuthenticated)
        {
            LocationCheckin curUserLocation = GatsbApplication.Current.GetUserLocation(GatsbApplication.Current.CurrentUser);
            presenceStatusDiv.Visible = (curUserLocation.LocationSpecified && curUserLocation.Location.Id == Location.Id);
        }
        else
        {
            presenceStatusDiv.Visible = false;
        }

        if (Location.Temporary)
        {
            GatsbWebApplication.SetPageName(this, string.Format("{0} - New Location", Location.Name));
        }
        else
        {
            GatsbWebApplication.SetPageName(this,
                string.Format("{0} - {1}, {2}, {3}", Location.Name, Location.Address.City, Location.Address.Region, Location.Address.PostalCode));
        }

        tempLocationData.Visible = Location.Temporary;
        locationData.Visible = !Location.Temporary;

        telNumberDiv.Visible = (Location.TelephoneNumber != PhoneNumber.Empty);

        GetPosts();
        GetVisitors();

        if (!IsPostBack)
        {
            if (_location.GeographicCoordinates != Decav.Geography.LatLongPoint.Empty)
            {
                areaMap.Pushpins.Add(new MapPushpin(_location.Id, _location.Name, _location.GeographicCoordinates));
                areaMap.MapCenter = _location.GeographicCoordinates;
            }
        }
    }

    protected string GetLocationAddressString()
    {
        if (Location.Address != null)
            return Location.Address.ToString().Replace("\r\n", "<br/>");
        else
            return string.Empty;
    }

    private void GetPosts()
    {
        using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
            eventHistory.DataSource = adapt.GetLatestLocationPosts(Location.Id, 50);

        eventHistory.DataBind();
    }

    private void GetVisitors()
    {
        User[] currentVisitors = null;
        User[] recentVisitors = null;

        using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
        {
            currentVisitors = adapt.GetLocationCurrentVisitors(Location.Id);
            recentVisitors = adapt.GetLocationRecentVisitors(Location.Id, DateTime.UtcNow.AddDays(GatsbConfigurationSection.Current.RecentWithinDays*-1));
        }

        if (currentVisitors.Length == 0)
        {
            currentVisitorsDiv.Visible = false;
        }
        else
        {
            currentVisitorsDiv.Visible = true;
            currentVisitorsRepeater.DataSource = currentVisitors;
            currentVisitorsRepeater.DataBind();
        }

        if (recentVisitors.Length == 0)
        {
            recentVisitorsDiv.Visible = false;
        }
        else
        {
            recentVisitorsDiv.Visible = true;
            recentVisitorsRepeater.DataSource = recentVisitors;
            recentVisitorsRepeater.DataBind();
        }
    }

    private Location _location;

    /// <summary>
    /// Gets the location represented on this page.
    /// </summary>
    public Location Location
    {
        get
        {
            if (_location == null)
            {
                using (IGatsbDataAdapter adapt = GatsbWebApplication.Current.GetDataAdapter())
                    _location = adapt.GetLocation(PlaceId);
            }

            return _location;
        }
    }

    /// <summary>
    /// Gets the unique identifier of the place that this page is for.
    /// </summary>
    public Guid PlaceId
    {
        get
        {
            if (Request.QueryString["id"] == null)
                Response.Redirect("~/Places.aspx", true);

            return new Guid(Request.QueryString["id"]);
        }
    }
}
